package models

import (
	"errors"
	"fmt"
	"github.com/ethereum/go-ethereum/log"
)

type User struct {
	Id           int64
	IdentityCard string `valid:"Required"`
	Password     string `valid:"Required"`
	Nickname     string `valid:"Required"`
}

//AddUser
//添加用户
func AddUser(user *User) int64 {
	insert, err := localdb.Insert(user)
	if err != nil {
		log.Error(err.Error())
	}
	fmt.Println(user.Id)
	return insert
}

// UpdateUser Update 修改用户信息
func UpdateUser(user *User) bool {
	_, err := localdb.Where("id = ?", user.Id).Update(user)
	if err != nil {
		log.Error(err.Error())
		return false
	}
	return true
}

func CheckPassword(user User) (result User, err error) {
	password := user.Password
	user.Password = ""
	_, err = localdb.Get(&user)
	if err != nil {
		return
	}
	if password != user.Password {
		err = errors.New("账号或密码错误")
	}
	result = user
	return
}

// SelectUserById 根据id查询用户
func SelectUserById(user *User) (err error) {
	_, err = localdb.Where("id = ?", user.Id).Get(user)
	return
}
